
var body= $("body");
//查询捐赠记录
$("#addAarea").click(function () {

    var name;
    var province = $("#province option:selected").val();
    var city = $("#city option:selected").val();
    var county = $("#county option:selected").val();
    //console.log(neighbourhood);
    city = city+county;
    //console.log(province+"  "+city);
    if(city===""){
        name = province;
    }else {
        name = city;
        //console.log(name);
    }
    var table = $("table tr:gt(0)");
    table.hide();
    var $d = table.filter(":contains('"+$.trim(name)+"')");
    $d.show();
});


body.on('click','.adminInfo',function () {
    var adminId = $(this).text();
    $.ajax({
        type: 'post',
        url: 'getAdminInfo',
        data:{
            'adminId':adminId
        },
        success:function (data) {
            //console.log(data);
            var adminBody = $("#adminBody");
            $ul = $("<ul class='list-group'></ul>");
            $ul.appendTo(adminBody);
            $ul.append("<li class='list-group-item'>姓名："+data.adminName+"</li>");
            $ul.append("<li class='list-group-item'>手机号："+data.phone+"</li>");
            $ul.append("<li class='list-group-item'>处理订单数："+data.oderNumber+"</li>");
            $ul.append("<li class='list-group-item'>地址："+data.address+"</li>");
            $ul.append("<li class='list-group-item'>工作区域："+data.workArea+"</li>");
        }
    })
});

//分配捐赠地区
$("#distribution").click(function () {
    var info = $("#table").bootstrapTable('getSelections');
    var recordId= [];
    //console.log(info);
    if(info.length===0){
        $("#message").append(" <div class=\"alert alert-danger alert-dismissable\">\n" +
            "                        <button type=\"button\" class=\"close\" data-dismiss=\"alert\"\n" +
            "                                     aria-hidden=\"true\">\n" +
            "                                     &times;\n" +
            "                         </button>\n" +
            "                         请选择捐赠记录再分配地区！！\n" +
            "                   </div>");
        return;
    }
    for (var i = 0;i<info.length;i++){
        if(info[i].state==="回收利用"){
            $("#message").append(" <div class=\"alert alert-danger alert-dismissable\">\n" +
                "                        <button type=\"button\" class=\"close\" data-dismiss=\"alert\"\n" +
                "                                     aria-hidden=\"true\">\n" +
                "                                     &times;\n" +
                "                         </button>\n" +
                "                         分配地区不能包含回收利用捐赠记录，请进行筛选再次操作！！\n" +
                "                   </div>");
            return;
        }
        recordId.push(info[i].recordId);
    }
    //console.log(recordId);
    $("#message").empty();
    $.ajax({
        type: 'post',
        dataType: "json",
        url: 'distributionArea',
        traditional: true,
        data: {
            'recordId': recordId
        },
        success:function (data) {
            if(data===1){
                window.location.href="/distributionMaterials"
            }
        }
    })

});

$("#recovery").click(function () {
    var recordId = [];
    var data = $("#table").bootstrapTable('getData');
    for (var i = 0;i<data.length;i++){
        if(data[i].state === "回收利用"){
            recordId.push(data[i].recordId);
        }
    }

    if(recordId.length===0){
        window.location.href="/discardedClothing";
        return;
    }
    $.ajax({
        type: 'post',
        dataType: "json",
        url: 'discardedClothing',
        traditional: true,
        data: {
            recordId: recordId,
        },
        success:function (result) {
            //console.log(result);
            if(result=== 1){
                window.location.href="/discardedClothing";
            }
        }
    })

});

    $('#table').bootstrapTable({
        classes: 'table table-bordered table-hover table-striped',
        url: 'getDonationRecordList',
        method: 'get',
        contentType:'application/json',
        dataType : 'json',        // 因为本示例中是跨域的调用,所以涉及到ajax都采用jsonp,
        uniqueId: 'recordId',
        idField: 'recordId',             // 每行的唯一标识字段
        cache: false,
        toolbar: '#toolbar',       // 工具按钮容器
        clickToSelect: false,     // 是否启用点击选中行
        showColumns: true,         // 是否显示所有的列
        showRefresh: true,         // 是否显示刷新按钮
        buttonsToolbar: '#buttonsToolbar',
        showToggle: true,        // 是否显示详细视图和列表视图的切换按钮(clickToSelect同时设置为true时点击会报错)

        pagination: true,                    // 是否显示分页
        sortOrder: "asc",                    // 排序方式
        sidePagination: "client",            // 分页方式：client客户端分页，server服务端分页
        pageNumber: 1,                       // 初始化加载第一页，默认第一页
        pageSize: 10,                        // 每页的记录行数     // 可供选择的每页的行数
        search: true,                      // 是否显示表格搜索，此搜索是客户端搜索

        //showExport: true,        // 是否显示导出按钮, 导出功能需要导出插件支持(tableexport.min.js)
        //exportDataType: "basic", // 导出数据类型, 'basic':当前页, 'all':所有数据, 'selected':选中的数据

        columns: [{
            checkbox:true,
            formatter:function (value,item) {
                //console.log(item);
                if(item.state === "未处理"||item.donationArea!=="暂未分配"){
                    return {
                        disabled: true,
                    }
                }else {
                    return {
                        disabled: false,
                    }
                }

            }
        },{
            field: 'recordId',
            title: '记录ID',
            sortable: true,
            align: 'center'
        }, {
            field: 'userId',
            title: '用户ID',
            sortable: true,
            formatter:function (value) {
                return "<button class='btn btn-default btn-sm userInfo' data-toggle='modal' data-target='#userInfo'>"+value+"</button>";
            }
        }, {
            field: 'donor',
            title: '捐赠人'
        }, {
            field: 'recycleId',
            title: '捐赠流水号',
            sortable: true
        },{
            field: 'startAddress',
            title: '起始位置'
        }, {
            field: 'jsonAddress',
            title: '目前位置',
            formatter:function (value,item) {
                if(item.jsonAddress!=="暂无"){
                    var json = JSON.parse(item.jsonAddress);
                    //console.log(json);
                    //console.log(json.length);
                    return json[json.length-1].address;
                }else {
                    return "暂无";
                }
            }
        }, {
            field: 'donationArea',
            title: '受捐地区'
        }, {
            field: 'donationName',
            title: '捐赠物'
        },{
            field: 'state',
            title: '记录状态',
            align: 'center',
            formatter:function(value){
                if(value === "未处理"){
                    return "<h4><span class='label label-warning'> 未处理 </span></h4>";
                }else if(value === "回收利用") {
                    return "<h4><span class='label label-danger'> 回收利用 </span></h4>";
                }else {
                    return "<h4><span class='label label-primary'> 捐赠 </span></h4>";
                }
            }
        }, {
            field: 'operator',
            title: '操作员',
            formatter:function (value) {
                if(value===null){
                    return "暂无"
                }else {
                    return value;
                }
            }
        },{
            field: 'joinTime',
            title: '创建时间',
            sortable: true,
            formatter:function (value) {
                return new Date(+new Date(new Date(value).toJSON()) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '');
            }
        },{
            field: 'updateTime',
            title: '更新时间',
            sortable: true,
            formatter:function (value) {
                return new Date(+new Date(new Date(value).toJSON()) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '');
            }
        }],
        onLoadSuccess: function(data){
            $("[data-toggle='tooltip']").tooltip();
        }
    });

